<template>
  <div class="app-container">
    <el-form :inline="true" :model="integral" class="demo-form-inline">
      <el-form-item style="margin-left: 32%;" label="所属用户">
        <el-select v-model="integral.userId" placeholder="请选择">
          <el-option
            v-for="item in users"
            :key="item.id"
            :label="item.account"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button @click="onClear">清空</el-button>
      </el-form-item>
    </el-form>
  <el-button type="primary" @click="linkToAdd">新增积分明细</el-button>
  <el-button type="danger" v-show="showDelBtn" @click="delIntegrals">删除</el-button>
  <el-table
      v-loading="listLoading"
      :data="integrals"
      element-loading-text="Loading"
      highlight-current-row
      @selection-change="handleSelectionChange">
  >
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="积分数量" width="300" align="center">
        <template slot-scope="scope">
          {{ scope.row.num }}
        </template>
      </el-table-column>
      <el-table-column label="积分信息" width="292" align="center">
        <template slot-scope="scope">
          {{ scope.row.numInfo }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>
            {{ scope.row.createTime }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="所属用户" width="250" align="center">
        <template slot-scope="scope">
          {{ scope.row.userId }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="操作" width="250">
        <template slot-scope="scope">
          <el-button title="编辑" type="primary" icon="el-icon-edit" circle @click="linkToEdit(scope.row.id)" />
          <el-button title="删除" type="danger" icon="el-icon-delete" circle @click="delIntegral(scope.row.id)" />
        </template>
      </el-table-column>
    </el-table>
    <nav style="text-align: center">
      <el-pagination
      background
      @current-change="handlePageChange"
      :current-page="pageNo"
      :page-size="pageSize"
      layout="prev, pager, next"
      :total="total">
    </el-pagination>
    </nav>
  </div>
  
</template>

<script>
import { getIntegralsApi, delIntegralApi, delIntegralsApi } from '@/api/integral'
import { getAllUsersApi } from "@/api/user"
export default {
  data() {
    return {
      imgPath:process.env.VUE_APP_BASE_API+'/',
      integral:{},
      pageNo: 1,
      pageSize: 10,
      integrals: [],
      users: [],
      listLoading: true,
      delIntegralArray:[],
      showDelBtn:false,
      total: 0
    }
  },
  created() {
    this.getIntegrals(this.pageNo, this.pageSize, this.integral)
    getAllUsersApi().then(response => {
        this.users = response.data
    })
  },
  methods: {
    handlePageChange(currentPage) {
      this.pageNo = currentPage;
      this.getIntegrals(this.pageNo, this.pageSize, this.integral)
    },
    handleSelectionChange(val) {
      if(val.length>0){
        this.showDelBtn=true
      }else{
        this.showDelBtn=false
      }
      this.delIntegralArray = val
    },
    delIntegrals(){
      this.listLoading = true
      delIntegralsApi(this.delIntegralArray)
        .then(response=>{
          this.getIntegrals(this.pageNo, this.pageSize)
          this.listLoading = false
        })
    },
    linkToAdd() {
      this.$router.push('/relation/integral/add')
    },
    onSearch(){
      this.pageNo = 1
      this.getIntegrals(this.pageNo, this.pageSize, this.integral)
    },
    onClear() {
      this.integral={}
      this.getIntegrals(this.pageNo, this.pageSize, this.integral)
    },
    getIntegrals(pageNo, pageSize, integral) {
      this.listLoading = true
      getIntegralsApi(pageNo, pageSize, this.integral)
        .then(response => {
          this.integrals = response.data.list
          this.pageNo = response.data.pageNum
          this.total = response.data.total
          this.listLoading = false
        })
    },
    linkToEdit(id) {
      this.$router.push({name:'editIntegral', params:{id:id}})
    },
    linkToShow(id) {
      this.$router.push({name:'showIntegral', params:{id:id}})
    },
    delIntegral(id) {
      this.listLoading = true
      delIntegralApi(id).then(response => {
        this.getIntegrals(this.pageNo, this.pageSize)
        this.listLoading = false
      })
    }
  }
}
</script>

